﻿using System;
using MaLu.Common;
using System.Data;
using MaLu.BusiLogic;
using Wicresoft.Framework.Organization;
using MaLu.Data;
using System.Web.UI.WebControls;

namespace MaluJg.web.StatsIn
{
    public partial class SList : SPage
    {

        public string SearchClauseItem
        {
            get
            {
                if (ViewState["SearchClauseItem"] == null)
                    ViewState["SearchClauseItem"] = "1=1";

                return ViewState["SearchClauseItem"].ToString();
            }
            set
            {
                ViewState["SearchClauseItem"] = value;

            }
        }
        public DataTable DTVillage
        {
            get
            {
                if (ViewState["DTVillage"] == null)
                    ViewState["DTVillage"] = BLFarm.GetFType(Provider);

                return (DataTable)ViewState["DTVillage"];
            }
            set
            {
                ViewState["DTVillage"] = value;

            }
        }
        protected string Type
        {
            get
            {

                return ddlType.SelectedValue;
            }
        
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "扶持资金列表";


            tbYear.Attributes.Add("readonly", "readonly");

            if (!IsPostBack)
            {
                this.Order.OrderColumn = "Year";
                this.Order.OrderDirection = "DESC";
                //Department mainD = OrganizationHelper.GetDepartmentInfo(CurrentUser.MainDepartmentID);
                //string[] ids = mainD.IDPath.Split('/');
                //if (ids.Length >= 4)
                //    DepartmentID = new Guid(ids[3]);
                //else
                //    DepartmentID = new Guid(ids[ids.Length - 1]);

                ddlType.DataSource = BLFarm.GetFType();
                ddlType.DataValueField = "value";
                ddlType.DataTextField = "text";
                ddlType.DataBind();
                ddlVillage.DataSource = DTVillage;
                ddlVillage.DataTextField = "name";
                ddlVillage.DataValueField = "id";
                ddlVillage.DataBind();

                ddlVillage.Items.Insert(0, new ListItem("全部","-1"));

            }

        }

        protected override void Pager_PageChanged(object sender, EventArgs e)
        {
            BindData();
            tbIDs.Text = string.Empty;
            base.Pager_PageChanged(sender, e);
        }

        protected override void Order_OrderChanged(object sender, EventArgs e)
        {
            BindData();
            base.Order_OrderChanged(sender, e);
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            Pager.PageIndex = 0; 
           BindData();
        }

        protected void lbtnDelete_Click(object sender, EventArgs e)
        {

            Pager.PageIndex = 0;

            if (tbIDs.Text.Trim() != string.Empty)
            {
                string table = "FBoothDetails";
                if (ddlType.SelectedValue == "fhuafei")
                {                  
                    table = "FFertilizerDetails";                   
                }
                if (ddlType.SelectedValue == "fcold")
                {                 
                    table = "FColdStorageDetails";                   
                }
                if (ddlType.SelectedValue == "fgreen")
                {                   
                    table = "FGreenHouseDetails";                    
                }
                if (ddlType.SelectedValue == "fmembrane")
                {                  
                    table = "FMembraneDetails";                    
                }
                if (ddlType.SelectedValue == "fpackage")
                {                   
                    table = "FPackageDetails";                  
                }
                string sql = string.Format("delete from [{0}] where id in ('{1}');", table, tbIDs.Text.Trim().Substring(1).Replace(",", "','"));

                SqlHelper.Instance.ExecuteNonQuery(Provider, sql);
                tbIDs.Text = string.Empty;
            }

            BindData();
        }
        protected void ddlType_Selected(object sender, EventArgs e)

        {
            Pager.PageIndex = 0;

            SearchClauseItem = "1=1";
            BindData();

        
        }
        private void BindData()
        {
            int recordCount = 0;
            string selectedFields = "ID,FamilyName,VillageID,Money,Year";
            string table = "FBoothDetails";
            string order = "Year desc,ID desc ";

            if (ddlType.SelectedValue == "fhuafei")
            {
                selectedFields = "ID,FamilyName,VillageID,Money,Year";
                table = "FFertilizerDetails";
                order = "Year desc,ID desc ";
            }

            if (ddlType.SelectedValue == "fcold")
            {
                selectedFields = "ID,FamilyName,VillageID,[Rent] as Money,Year";
                table = "FColdStorageDetails";
                order = "Year desc,ID desc ";
            }

            if (ddlType.SelectedValue == "fgreen")
            {
                selectedFields = "ID,FamilyName,VillageID,Money,Year";
                table = "FGreenHouseDetails";
                order = "Year desc,ID desc ";
            }


            if (ddlType.SelectedValue == "fmembrane")
            {
                selectedFields = "ID,FamilyName,VillageID,Money,Year";
                table = "FMembraneDetails";
                order = "Year desc,ID desc ";
            }


            if (ddlType.SelectedValue == "fpackage")
            {
                selectedFields = "ID,FamilyName,VillageID,Money,Year";
                table = "FPackageDetails";
                order = "Year desc,ID desc ";
            }



            DataTable DTMyProposal = BLCommon.GetPagingList(Provider, selectedFields, table, SearchClauseItem, order, Pager.PageIndex + 1, Pager.PageSize, false, 0, ref recordCount);
            DTMyProposal.Columns.Add("VName");
            foreach (DataRow item in DTMyProposal.Rows)
            {
                DataRow[] drs = DTVillage.Select("id=" + item["VillageID"].ToString());
                if (drs.Length > 0)
                    item["VName"] = drs[0]["Name"].ToString();
            }
            
            Pager.RecordCount = recordCount;
            rptBusi.DataSource = DTMyProposal;
            rptBusi.DataBind();
        }

        protected void btnAdvancedSearch_Click(object sender, EventArgs e)
        {
            string where = "1=1";
            if (tbName.Text.Trim() != string.Empty)
            {
                where += " and FamilyName like '%" + tbName.Text.Trim().Replace("'", "''") + "%'";

            }
            if (tbYear.Text.Trim() != string.Empty)
            {
                where += " and Year='" + tbYear.Text.Trim() + "'";

            }
            if (ddlVillage.SelectedValue != "-1")
            {
                where += " and VillageID <= '" + ddlVillage.SelectedValue + "'";

            }
            SearchClauseItem = where;
            Pager.PageIndex = 0;
            
         BindData();
        }
    }
}
